<template>
	<view class="bgMinor2 pt-24 plr-24 pt-24" :style="{ minHeight: `${windowHeight}px`, height: 'auto' }">
		<view>

			<view class="bgWhite plr-24 radius-16">

				<view class="flex ptb-24 border-b-2">
					<view class="size-28 width-160">问题描述</view>
					<view class="flex-1">
						<uv-input placeholder="点击输入问题描述" border="none" v-model="dataInfo.username"></uv-input>
					</view>
				</view>
				<view class="flex ptb-24 border-b-2">

					<view class="size-28 width-160">
						图片上传
					</view>
					<view class="flex-1">

						<view style="width: 70px;position: relative;" v-if="dataInfo && dataInfo.fileList1 !=''">
							<img style="width: 70px;height: 70px;" :src="dataInfo.fileList1"></img>
							<!-- <img @click.stop="dataInfo.fileList1 = ''" src="@/static/images/del.png" style="cursor: pointer;position: absolute; top: 1px;
						z-index: 32 !important;
						right:   0;width: 20px;height: 20px;"> -->
						</view>
							
							<view @click.stop="afterRead" style="width: 70px;height: 70px;" v-else class="itemClass pointer relative radius-16 hidden bgMinor5 center">
								<view class="center">
									<uv-icon name="plus" color="#E1E1E1" size="50rpx" bold></uv-icon>
								</view>
							</view>
					</view>
				</view>
			</view>
		</view>
		<view class="mt-30">
			<view class="width100 height-90"></view>
			<view class="fixedClass plr-24">
				<view class="list_b_btn " style="background: #007DF5;" @click="onSubmit">提交</view>
			</view>
			
			
			
		</view>
	</view>
</template>

<script setup lang="ts">
	// import { baseURL } from '@/utils/config.ts';
	import { reactive } from 'vue';
	import { getWindowHeight } from '@/pages/util/getHeight';
	import { onLoad } from '@dcloudio/uni-app';
	import { get, post, baseConfig } from '@/utils/request';

	import { addOpen, loggedIn, addOpenTab } from '@/pages/util/hooks';
	import useLoginStore from '@/pinia-store/userInfoStore';
	const useLogin = useLoginStore();
	// console.log('useLoginuseLogin',useLogin);
	const windowHeight = getWindowHeight();
	console.log('aaaaaaaaaaaaaa', windowHeight);
	/** @name 数据 */
	const dataInfo = reactive({
		username: '',
		fileList1: [],
		fileList2: []
	});
	onLoad((options : any) => { });
	/** @name xxx */

	const onSubmit = async () => {

		try {
			let data = {
				"content": dataInfo.username,
				"reflectImg": dataInfo.fileList1
			}
			const response = await post<{
				content : string;
				reflectImg : string;
			}>('/feedback/add', data);
			console.log(response)
			if(response){
				uni.redirectTo({
					url: '/pages/mine/set'
				});
			}
			
		} catch (error) {
			console.error(error);
		}
	};
	// 删除图片
	function deletePic(event) {
		dataInfo[`fileList${event.name}`].splice(event.index, 1);
	}

	// 新增图片
	async function afterRead(event) {
		console.log('新增图片event', event);
		// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
		// let lists = [].concat(event.file);
		// let fileListLen = dataInfo[`fileList${event.name}`].length;
		// lists.map((item) => {
		// 	dataInfo[`fileList${event.name}`].push({
		// 		...item,
		// 		status: 'uploading',
		// 		message: '上传中'
		// 	});
		// });
		uni.chooseImage({
			count: 1, // 默认9
			sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
			sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
			success: (res) => {
				// dataInfo[`fileList${event.name}`].splice(
				// 	fileListLen,
				// 	1,
				// 	Object.assign(item, {
				// 		status: 'success',
				// 		message: '',
				// 	})
				// );
				
				uni.uploadFile({
					url: baseURL + 'sys/common/upload', // 替换为实际的上传接口地址
					filePath: res.tempFilePaths[0],
					name: 'file',
					header: {
						'X-Access-Token': uni.getStorageSync('token')
					},
					formData: {
						// 这里可以添加其他表单数据
						file: res.tempFiles[0],
					},
					success: (uploadRes) => {
						console.log('上传成功', JSON.parse(uploadRes.data).message);
						// fetchData(uploadRes)
						dataInfo.fileList1 = JSON.parse(uploadRes.data).message
					},
					fail: (err) => {
						console.error('上传失败', err);
					}
				});
			}
		});
	}
	function uploadFilePromise(url) {
		return new Promise((resolve, reject) => {
			let a = uni.uploadFile({
				url: 'http://192.168.2.21:7001/upload', // 仅为示例，非真实的接口地址
				filePath: url,
				name: 'file',
				formData: {
					user: 'test'
				},
				success: (res) => {
					setTimeout(() => {
						resolve(res.data);
					}, 1000);
				}
			});
		});
	}
</script>

<style scoped lang="scss">
	// :deep(.checklist-box) {
	// 	//  模版
	// }

	:deep(.uv-upload__button) {
		margin: 0;
	}

	// 按钮
	.list_b_btn {
		background: -webkit-linear-gradient(left top, #ffc800, #ff0053, #ff5037); // , 100%,#ff5037 left top,
	}
</style>